package action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;

import utilidades.Constants;
import vo.AlineacionVO;
import vo.PlantillaVO;
import vo.UsuarioVO;
import bo.AlineacionBO;
import form.DatosUsuarioForm;

public class DatosUsuarioAction extends LookupDispatchAction{

	public ActionForward newSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		final UsuarioVO tmpUsuario = (UsuarioVO) request.getSession()
				.getAttribute(Constants.USUARIO_ACTUAL);
		final AlineacionBO tmpAlineacionBo = new AlineacionBO();
		final PlantillaVO tmpPlantilla = tmpAlineacionBo
				.getPlantilla(tmpUsuario.getNick());
		final AlineacionVO tmpAlineacionIdeal = tmpAlineacionBo
				.getAlineacionIdeal(1, tmpUsuario.getNick());
		final AlineacionVO tmpAlineacion = tmpAlineacionBo.getAlineacion(tmpUsuario);
		request.getSession().setAttribute("Alineacion", tmpPlantilla);
		request.getSession().setAttribute("OnceIdeal", tmpAlineacion);
		//request.getSession().setAttribute("OnceIdeal", tmpAlineacionIdeal);
		return mapping.findForward("success");
	}
	
	public ActionForward guardarAlineacion(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		final PlantillaVO tmpPlantilla = (PlantillaVO) request.getSession().getAttribute("Alineacion");
		final AlineacionBO tmpAlineacionBo = new AlineacionBO();
		tmpAlineacionBo.guardarAlineacion(tmpPlantilla, ((DatosUsuarioForm) form)
				.getAlineado(), (UsuarioVO) request.getSession().getAttribute(
				Constants.USUARIO_ACTUAL));
		
		return this.newSearch(mapping, form, request, response);
	}

	@Override
	protected Map getKeyMethodMap() {
		// TODO Auto-generated method stub
		Map map=new HashMap();		
		map.put("action.newSearch","newSearch");
		map.put("label.guardarAlineacion","guardarAlineacion");
		
		return map;
	}


}
